﻿module Enferno.Public.Web.Components {
    
    export class BasketServices {
        public static AddItem = "api/shoppingbasket/{0}/addItem";
        public static UpdateItem = "api/shoppingbasket/{0}/updateItem";
        public static DeleteItem = "api/shoppingbasket/{0}/deleteItem";
        public static ApplyDiscount = "api/shoppingbasket/{0}/applyDiscount";
        public static RemovePromotion = "api/shoppingbasket/{0}/removePromotion";
        public static GetBasket = "api/shoppingbasket/get";
    }

    export class CheckoutServices {
        public static GetCheckout = "api/checkout/get";
        public static UpdatePrivateInformation = "api/checkout/{0}/updateprivateinformation";
        public static UpdateCompanyInformation = "api/checkout/{0}/updatecompanyinformation";
        public static SetDelivery = "api/checkout/{0}/setdelivery";
        public static SetPayment = "api/checkout/{0}/setpayment";
        public static PrivatePurchase = "api/checkout/{0}/privatepurchase";
        public static CompanyPurchase = "api/checkout/{0}/companypurchase";
    }

    export class AccountServices {
        public static Login = "api/account/login";
        public static Logout = "api/account/logout";
        public static GetCustomerInfo = "api/account/getPersonInfo";
        public static GetCompanyInfo = "api/account/getCompanyInfo";
    }

    export class UrlHelper {

        public static baseUrl = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '') + '/';

        public static getUrl(path: string, inserts?: string[]): string {
            var url = UrlHelper.baseUrl + path;
            if (inserts != null)
                url = StringHelper.format(url, inserts);

            return url;
        }

        public static redirect(targetUrl: string, values: any, method: string, target: string = "_self"): void {

            method = (method && method.toUpperCase() == 'GET') ? 'GET' : 'POST';

            if (!values) {
                var obj = UrlHelper.parseUrl(targetUrl);
                targetUrl = obj.url;
                values = obj.params;
            }

            var form = $('<form>', {
                attr: {
                    method: method,
                    action: targetUrl,
                    target: target
                }
            });

            for (var i in values) {
                $('<input>', {
                    attr: {
                        type: 'hidden',
                        name: i,
                        value: values[i]
                    }
                }).appendTo(form);

            }

            $('body').append(form);
            form.submit();
        }

        private static parseUrl(url: string): any {
            if (url.indexOf('?') == -1)
                return { url: url, params: {} }

            var parts = url.split('?');
            var baseUrl = parts[0];
            var queryString = parts[1];

            var returnObj: any = {};
            var elems = queryString.split('&');

            var obj = {};

            for (var i in elems) {
                var elem = elems[i];
                var pair = elem.split('=');
                obj[pair[0]] = pair[1];
            }

            returnObj.url = baseUrl;
            returnObj.params = obj;

            return returnObj;
        }

    }

    export class StringHelper {
        public static format(str: string, args: any[]): string {
            if (args != null) {
                for (var i = 0; i < args.length; i++) {
                    str = str.replace(
                        RegExp("\\{" + i + "\\}", 'g'), args[i].toString());
                }
            }
            return str;
        }
    }

} 